package com.hd.homework0412.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hd.homework0412.dao.ProductDao;
import com.hd.homework0412.dao.ProductImgDao;
import com.hd.homework0412.entity.Category;
import com.hd.homework0412.entity.Product;
import com.hd.homework0412.service.CategoryService;
import com.hd.homework0412.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    ProductService productService;

    @Autowired
    ProductDao productDao;
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    ProductImgDao productImgDao;

    @GetMapping("/getAll")
    public List<Category> getAll(){
        List<Category> categories  =categoryService.getAll();
        return categories;
    }
    @GetMapping("/t")
    public HashMap<String,Object> topThree(){
        HashMap<String,Object> map=new HashMap<>();
        List<Product> products = productService.topThree();
        map.put("msg","ok");
        map.put("code","1");
        map.put("data",products);
        return map;
    }
    @GetMapping("/getAll1")
    public HashMap<String,Object> get(){
        HashMap<String, Object> hashMap = new HashMap<>();
        List<Category> categories = categoryService.list();
        for (Category category : categories) {
            QueryWrapper<Category> wrapper = new QueryWrapper<>();
            wrapper.eq("parent_id",category.getCategoryId());
            List<Category> categoryList = categoryService.list(wrapper);
            category.setCategories(categoryList);
            for (Category category1 : categoryList) {
                QueryWrapper<Category> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("parent_id",category1.getCategoryId());
                List<Category> list = categoryService.list(wrapper1);
                category1.setCategories(list);
            }
        }
        hashMap.put("Msg","ok");
        hashMap.put("Code","10000");
        hashMap.put("Date",categories);
        return hashMap;
    }

    /*@GetMapping("/getAll0")
    public List<Category> getAll0(Integer level){
        return categoryService.getAll0(level);
    }*/
    @GetMapping("/p")
    public Map<String, Object> p() {
        Map<String, Object> map = new HashMap<>();
        //获取最新的三款产品
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time")
                .last("limit 3");
        List<Map<String, Object>> products = productDao.selectMaps(wrapper);

        //获取全部图片
        List<Map<String, Object>> productImgs = productImgDao.selectMaps(null);

        products.forEach(product -> {
            //遍历 products 产品
            product.put("imgs",new ArrayList<Map<String, Object>>());
            productImgs.forEach(productImg -> {
                //遍历 productImgs 图片
                if (product.get("product_id").equals(productImg.get("item_id"))){
                    //productId == itemId
                    ((ArrayList<Map<String, Object>>)product.get("imgs")).add(productImg);
                }
            });
        });
        map.put("msg", "ok");
        map.put("code", 10000);
        map.put("data", products);
        return map;
    }

}
